package com.shxc.smarttrade.old.utils;

import android.app.Activity;

import java.util.Stack;

/**
 * Created by LYS on 2016/10/18.
 */
public class CloseAllActivity {
    private static Stack<Activity> mActivityStack = new Stack<Activity>();
    private static CloseAllActivity instance = new CloseAllActivity();

    private CloseAllActivity() {
    }

    public static CloseAllActivity getScreenManager() {
        return instance;
    }

    // 弹出当前activity并销毁
    public void popActivity(Activity activity) {
        if (activity != null) {
            Logger.e("pop:", activity.getClass().getName());
            activity.finish();
            mActivityStack.remove(activity);
            activity = null;
        }
    }

    // 将当前Activity推入栈中
    public void pushActivity(Activity activity) {
        Logger.e("push:", activity.getClass().getName());
        mActivityStack.add(activity);
    }

    // 退出栈中所有Activity
    public void clearAllActivity() {
        while (!mActivityStack.isEmpty()) {
            Activity activity = mActivityStack.pop();
            if (activity != null) {
                activity.finish();
            }
        }
    }

    public Activity getFirstActivity() {
        if (mActivityStack.size() > 0)
            return mActivityStack.get(mActivityStack.size() - 1);
        else return null;
    }

    public boolean isEmpty() {
        if (mActivityStack.size() == 0)
            return true;
        else return false;
    }


    public void clearEcxeptMainActivity() {

        for (int i = 0; i < mActivityStack.size() - 1; i++) {
            Activity activity = mActivityStack.pop();
            if (activity != null) {
                activity.finish();
            }
        }
//        while (!mActivityStack.isEmpty()) {
//            if (mActivityStack.size() == 1) {
//
//            } else {
//                Activity activity = mActivityStack.pop();
//                if (activity != null) {
//                    activity.finish();
//                }
//            }
//        }
    }

    public int getStackSize() {
        if (!mActivityStack.empty())
            return mActivityStack.size();
        return 0;
    }
}
